package com.leyou.item.service.impl;

import com.leyou.common.enums.LyExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
private CategoryMapper categoryMapper;
    @Override
    public List<CategoryDTO> queryByParentId(Long pid) {
        Category category = new Category();
        category.setParentId(pid);
        List<Category> list = categoryMapper.select(category);
        if(CollectionUtils.isEmpty(list)){
            throw  new LyException(LyExceptionEnum.GOODS_NOT_FOUND);
        }

        return  BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }

    @Override
        public List<CategoryDTO> queryListByBrandId(Long brandId) {
            List<Category> list = categoryMapper.queryByBrandId(brandId);
            // 判断结果
            if(CollectionUtils.isEmpty(list)){
                throw new LyException(LyExceptionEnum.CATEGORY_NOT_FOUND);
            }
            // 使用自定义工具类，把Category集合转为DTO的集合
            return BeanHelper.copyWithCollection(list, CategoryDTO.class);
        }

    @Override
    public List<CategoryDTO> queryCategoryByIds(List<Long> ids) {
        List<Category> list = categoryMapper.selectByIdList(ids);

        if (CollectionUtils.isEmpty(list)) {

            throw new LyException(LyExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);

    }

}
